//==========================================================================
// Copyright (c) 2000-2008,  Elastos, Inc.  All Rights Reserved.
//==========================================================================

#include <ddk.h>

#include "ddk_tst_print.h"

class PreemptionServer001 : public CDeviceDriver
{
public:
    CARAPI Read(
        /* [in] */ Int64 u64Offset,
        /* [in] */ Int32 bytesToRead,
        /* [out] */ EzByteBuf buffer,
        /* [out] */ IEvent * * ppCompletionEvent);

    CARAPI Write(
        /* [in] */ Int64 u64Offset,
        /* [in] */ EzByteBuf buffer,
        /* [out] */ Int32 * pBytesWritten,
        /* [out] */ IEvent * * ppCompletionEvent);

    CARAPI Control(
        /* [in] */ Handle32 nControlCode,
        /* [in] */ EzByteBuf inBuffer,
        /* [out] */ EzByteBuf * pOutBuffer,
        /* [out] */ IEvent * * ppCompletionEvent);

    virtual void Dispose() {}
};

EXTERN IDeviceDriver * CDECL CreatePreemptionServer0(uint_t uDeviceNo,
    void *pvParameter)
{
    PreemptionServer001 *pDriver = new PreemptionServer001;
    if (NULL == pDriver) {
        kprintf("ERROR:There have no enough memory, %s, %d\n",
                        __FILE__, __LINE__);
        return NULL;
    }

    pDriver->AddRef();

    return pDriver;
}

ECode PreemptionServer001::Read(
    /* [in] */ Int64 u64Offset,
    /* [in] */ Int32 bytesToRead,
    /* [out] */ EzByteBuf buffer,
    /* [out] */ IEvent * * ppCompletionEvent)
{
    return E_NOT_IMPLEMENTED;
}

ECode PreemptionServer001::Write(
    /* [in] */ Int64 u64Offset,
    /* [in] */ EzByteBuf buffer,
    /* [out] */ Int32 * pBytesWritten,
    /* [out] */ IEvent * * ppCompletionEvent)
{
    return E_NOT_IMPLEMENTED;
}

ECode PreemptionServer001::Control(
    /* [in] */ Handle32 nControlCode,
    /* [in] */ EzByteBuf inBuffer,
    /* [out] */ EzByteBuf * pOutBuffer,
    /* [out] */ IEvent * * ppCompletionEvent)
{
    if (NULL != ppCompletionEvent || !pOutBuffer->IsNull()) {
        return E_INVALID_ARGUMENT;
    }

    ECode ec = NOERROR;

    switch (nControlCode) {
        case 0:{
            if (inBuffer.IsNull()) {
                return E_INVALID_ARGUMENT;
            }

            bool_t bOldSchedule = DzDisablePreemption();
            DDK_PRINT_BLOCK_START("schd001.out");
            kprintf("DisablePreemption\n");
            DzDelay(1000 * (*(uint_t *)(char *)inBuffer));
            kprintf("RestorePreemption\n");
            DDK_PRINT_BLOCK_END();
            DzRestorePreemption(bOldSchedule);

            break;
        }

        case 1:{
            if (!inBuffer.IsNull()) {
                return E_INVALID_ARGUMENT;
            }

            DzDisablePreemption();
            kprintf("DisablePreemption\n");

            break;
        }

        default:{
            return E_NOT_IMPLEMENTED;
        }
    }

    return ec;
}
